#include <pfcGlobal.h>
#include <wfcClient.h>
#include <uifcDefaultListeners.h>
#include <logger.h>
#include <wxyutils.h>
#include "main.h"
#include <fstream>
#include <io.h>
#include <windows.h>
#include <vector>
#include <iterator>
#include <algorithm>
#ifdef GetMessage
#define GetMessage GetMessage
#endif

wfcStatus ColorPart();
wfcStatus RandColor();
wfcStatus TransPart();
//程序入口
extern "C" int user_initialize(
    int argc,
    char *argv[],
    char *version[],
    char *build,
    wchar_t errbuf[80])
{

    GetDllPath(dllpath);
    for (int i = 0; i < 3; i++)
    {
        LogFile[i] = dllpath + LogFile[i];
    }
    initLogger(LogFile[0], LogFile[1], LogFile[2]);

    pfcSession_ptr session = pfcGetProESession();
    try
    {
        //名字首尾字母不要重复，避免快捷键冲突
        xstring Command[] = {"ColorPart", "RandColor", "TransPart"};
        myCallback ComClass[] = {ColorPart, RandColor, TransPart};

        for (int i = 0; i < sizeof(Command) / sizeof(Command[0]); i++)
        {
            xstring ComName = Command[i] + "_com";
            xstring Mapkey = "o" + Command[i].Substring(0, 1) + Command[i].Substring(Command[i].GetLength() - 1, -1);
            CreateUICommand(session, ComName, new MycallbackClass(ComClass[i]),
                            MSGFILE, Command[i], Command[i], Command[i],
                            new MyActionAccess(pfcACCESS_AVAILABLE));
            //设置快捷键
            session->SetConfigOption("mapkey", Mapkey + " @MAPKEY_LABEL" + Command[i] + "; ~ Command `" + ComName + "`;");
        }
        session->SetConfigOption("mapkey", "st @MAPKEY_LABEL启动" + xstring(ProjectName) +
                                               "; ~ Activate `main_dlg_cur` `page_Tools_control_btn` 1; ~ Command `ProCmdUtilAux`; ~ Select `aux_apps` `AppList` 1 `" +
                                               xstring(ProjectName) + "`; ~ Activate `aux_apps` `StartBtn`; ~ Activate `aux_apps` `CloseBtn`;");
        session->SetConfigOption("mapkey", "sp @MAPKEY_LABEL停止" + xstring(ProjectName) +
                                               "; ~ Activate `main_dlg_cur` `page_Tools_control_btn` 1; ~ Command `ProCmdUtilAux`; ~ Select `aux_apps` `AppList` 1 `" +
                                               xstring(ProjectName) + "`;~ Activate `aux_apps` `StopBtn`; ~ Activate `aux_apps` `CloseBtn`;");
    }
    OTK_EXCEPTION_PRINT_LOG
    catch (const std::exception &e)
    {
        std::cerr << e.what() << '\n';
    }

    return 0;
}
//程序出口
extern "C" void user_terminate()
{
    LOG(LOG_FATAL); //触发logger析构函数
    fstream file;
    for (int i = 0; i < 3; i++)
    {
        file.open(LogFile[i], ios::in);
        if (file.get() == EOF)
        {
            file.close();
            file.clear();
            _FileExt emptyfile(LogFile[i].c_str());
            DeleteFileA(emptyfile.fullname.c_str());
        }
        else
        {
            file.close();
            file.clear();
        }
    }
}
